/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014 abel533@gmail.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package org.ypx.center.service.impl;

import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.ypx.center.service.BaseService;
import org.ypx.shared.common.Message;
import org.ypx.shared.common.Result;
import tk.mybatis.mapper.common.Mapper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tingwen on 2017/4/18.
 */
@Service
@Transactional
public abstract class BaseServiceImpl<T> implements BaseService<T> {

    protected Logger logger = LoggerFactory.getLogger(getClass());

    protected Result<List<T>> result = new Result<>();

    @Autowired
    protected Mapper<T> mapper;

    public Mapper<T> getMapper() {
        return mapper;
    }

    @Override
    public Result<List<T>> save(T entity) {
        if (isExist(entity)) {
            result.setMessage(Message.USER_EXIST);
            return result;
        }
        if (!isSuccess(mapper.insertSelective(entity))) {
            result.setMessage(Message.FAILE);
        } else {
            result.setMessage(Message.SUCCESS);
        }
        return result;
    }

    @Override
    public Result<List<T>> delete(Object key) {
        if (!isExist((T) key)) {
            result.setMessage(Message.NO_OBJECT);
            return result;
        }
        if (!isSuccess(mapper.deleteByPrimaryKey(key))) {
            result.setMessage(Message.FAILE);
        } else {
            result.setMessage(Message.SUCCESS);
        }
        return result;
    }

    @Override
    public Result<List<T>> updateAll(T entity) {
        if (!isExist(entity)) {
            result.setMessage(Message.NO_OBJECT);
            return result;
        }
        if (!isSuccess(mapper.updateByPrimaryKey(entity))) {
            result.setMessage(Message.FAILE);
        } else {
            result.setMessage(Message.SUCCESS);
        }
        return result;
    }

    @Override
    public Result<List<T>> updateNotNull(T entity) {
        if (!isExist(entity)) {
            result.setMessage(Message.NO_OBJECT);
            return result;
        }
        if (!isSuccess(mapper.updateByPrimaryKeySelective(entity))) {
            result.setMessage(Message.FAILE);
        } else {
            result.setMessage(Message.SUCCESS);
        }
        return result;
    }

    @Override
    public Result<List<T>> selectByKey(Object key) {
        List<T> object = new ArrayList<>();
        object.add(mapper.selectByPrimaryKey(key));
        result.setData(object);
        return result;
    }

    @Override
    public Result<List<T>> selectByExample(Object example) {
        List<T> objects = mapper.selectByExample(example);
        result.setData(objects);
        return result;
    }

    @Override
    public Result<List<T>> select(T entity) {
        List<T> objects = mapper.select(entity);
        result.setData(objects);
        return result;
    }

    @Override
    public Result<List<T>> selectByUser(Object example, int page, int rows) {
        //分页查询
        PageHelper.startPage(page, rows);
        return select((T) mapper);
    }


    @Override
    public boolean isSuccess(int flag) {
        if (flag != 1)
            return false;
        return true;
    }

    @Override
    public boolean isExist(T example) {
        List<T> users = mapper.select(example);
        if (users.size() == 0)//不存在
            return false;
        return true;
    }
}
